package org.xiaojinlong.algo;

/**
 * @author Jin Long
 *         2016/6/27
 */
public class Selection {
    static <Item> boolean notNull(Comparable<Item>[] a) {
        for (Comparable<Item> item : a) {
            if (item == null) {
                return false;
            }
        }

        return true;
    }

    public static <Item> void sort(Comparable<Item>[] a) {
        assert notNull(a) : "要排序的数组不应该有null值！";

        // 升序排列
        int len = a.length;
        for (int i = 0; i < len; i++) {
            int min = i;
            for (int j = i + 1; j < len; j++) {
                if (a[j].compareTo((Item) a[i]) < 0) {
                    min = j;
                }
            }

            Comparable<Item> tmp = a[i];
            a[i] = a[min];
            a[min] = tmp;
        }
    }

    public static void main(String[] args) {
        Comparable<Integer>[] test = new Comparable[2];
        test[0] = 3;
        test[1] = 2;
        sort(test);
    }
}
